/**
 * 抽取出来方便
 */
import * as renderUtil from '@/libs/renderUtil.js'
import constants from '@/constants/constants'
import operate from './operate.vue';
import dictLabel from '@/views/common/dict/DictLabel'
import dictSelect from '@/views/common/dict/DictSelect'
import dictCheckbox from '@/views/common/dict/DictCheckBox'

const searchFormRules = {
    name: [
        {
            validator(rule, value, callback, source, options) {
                var errors = []
                if (value.length > 20) {
                    errors.push(new Error('不能超过20字长', rule.field))
                }
                callback(errors)
            }
        }
    ],
    mail: [
        {type: 'email', message: 'Incorrect email format', trigger: 'blur'}
    ]
}

const dictKinds = {
    LOCAL_LABEL_DICT: 'local_label_dict'
}
const localDicts = {
    dictKinds,
    dictData: {
        [dictKinds.LOCAL_LABEL_DICT]: [{
            kind: dictKinds.LOCAL_LABEL_DICT,
            itemKey: '1',
            'itemName': '标签1',
            'parentKind': '',
            'parentKey': '',
            'sortNum': 1
        }, {
            kind: dictKinds.LOCAL_LABEL_DICT,
            itemKey: '2',
            'itemName': '标签2',
            'parentKind': '',
            'parentKey': '',
            'sortNum': 1
        }
        ]
    }
}

let tableDefine = {
    data() {
        return {
            loading: false,
            columns: [
                {
                    title: 'Name',
                    key: 'name'
                },
                {
                    title: 'Age',
                    key: 'age'
                },
                {
                    title: 'Address',
                    key: 'address'
                },
                {
                    title: 'created',
                    key: 'created',
                    render: (h, params) => {
                        return h('div', renderUtil.formatDateTime(params.row.created))
                    }
                },
                {
                    title: 'gender',
                    render: (h, params) => {
                        return h(dictLabel, {
                            props: {
                                value: params.row['gender'],
                                kind: constants.dicts.dictKinds.STD_GENDER,
                                defValue: '1',
                                showStyle: true
                            }
                        })
                    }
                },
                {
                    title: 'label',
                    render: (h, params) => {
                        return h(dictLabel, {
                            props: {
                                value: params.row['label'],
                                defValue: '1',
                                showStyle: true,
                                localDictItems: localDicts.dictData[dictKinds.LOCAL_LABEL_DICT]
                            }
                        })
                    }
                },
                {
                    title: 'Action',
                    key: 'action',
                    width: 250,
                    align: 'center',
                    render: (h, params) => {
                        return h(operate, {
                            props: {
                                item: params.row
                            },
                            on: {
                                'refreshList': () => {
                                    this.loadData()
                                },
                                'updateItem': (item) => {
                                    this.editItem(item)
                                }
                            }
                        });
                    }
                }
            ],
            queryResult: {
                dataList: [],
                pageQuery: {
                    totalCount: 0,
                    pageCount: 0,
                    pageNo: 1,
                    pageSize: 10
                }
            },
            formSearch: {
                name: '',
                mail: '',
                city: 1,
                gender: '1'
            },
            searchFormRules: searchFormRules,
            constants,
            localDicts
        }
    },
    mounted() {
        this.loadData()
    },
    components: {
        operate,
        dictLabel,
        dictSelect,
        dictCheckbox
    }
}
export default tableDefine;
